In [7]:
import os
import matplotlib.pyplot as plt
import librosa, librosa.display
import IPython.display as ipd
import numpy as np
In [8]:
BASE_FOLDER = "audio/"
violin_sound_file = "violin_c.wav"
piano_sound_file = "piano_c.wav"
sax_sound_file = "sax.wav"
noise_sound_file = "noise.wav"
In [9]:
ipd.Audio(os.path.join(BASE_FOLDER, violin_sound_file)) 
Out[9]:
Your browser does not support the audio element.
In [10]:
ipd.Audio(os.path.join(BASE_FOLDER, piano_sound_file)) 
Out[10]:
Your browser does not support the audio element.
In [11]:
ipd.Audio(os.path.join(BASE_FOLDER, sax_sound_file)) 
Out[11]:
Your browser does not support the audio element.
In [12]:
ipd.Audio(os.path.join(BASE_FOLDER, noise_sound_file)) 
Out[12]:
Your browser does not support the audio element.
In [13]:
# load sounds
violin_c4, sr = librosa.load(os.path.join(BASE_FOLDER, violin_sound_file))
piano_c5, _ = librosa.load(os.path.join(BASE_FOLDER, piano_sound_file))
sax_c4, _ = librosa.load(os.path.join(BASE_FOLDER, sax_sound_file))
noise, _ = librosa.load(os.path.join(BASE_FOLDER, noise_sound_file))
In [14]:
len(violin_c4)
Out[14]:
59772
In [15]:
X = np.fft.fft(violin_c4)
len(X)
Out[15]:
59772
In [51]:
def plot_magnitude_spectrum(signal, sr, title, f_ratio=1):
    X = np.fft.fft(signal)
    X_mag = np.absolute(X)
    
    plt.figure(figsize=(18, 5))
    
    f = np.linspace(0, sr, len(X_mag))
    f_bins = int(len(X_mag)*f_ratio)  
    
    plt.plot(f[:f_bins], X_mag[:f_bins])
    plt.xlabel('Frequency (Hz)')
    plt.title(title)
In [52]:
plot_magnitude_spectrum(violin_c4, sr, "violin", 0.1)
No description has been provided for this image
In [53]:
plot_magnitude_spectrum(piano_c5, sr, "piano", 0.1)
No description has been provided for this image
In [54]:
plot_magnitude_spectrum(sax_c4, sr, "sax", 0.1)
No description has been provided for this image
In [55]:
plot_magnitude_spectrum(noise, sr, "noise", 0.1)
No description has been provided for this image
In [ ]: